package com.jacklin.order.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;

import com.jacklin.order.domain.Orders;
import com.jacklin.order.feignClient.ProductServiceFeignClient;
import com.jacklin.order.mapper.OrdersMapper;
import com.jacklin.order.service.OrderService;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.jacklin.common.api.vo.Result;
import org.jacklin.common.dto.OrderDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 订单服务实现类
 *
 * @author: jacklin
 * @since: 2022/3/26 12:04
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private ProductServiceFeignClient productServiceFeignClient;

    @Override
    public Result<?> createOrder(OrderDTO orderDTO) {
        Result<Object> responseResult = new Result<>();
        log.info("全局事务ID:{}", RootContext.getXID());
        try {
            //用户使用优惠券

            //账户扣款

            //判断扣款状态

            //创建订单
            log.info("------------- 下单开始 -------------");
            Orders orders = Orders.buildOrder(orderDTO);
            ordersMapper.insert(orders);

            //RPC远程调用库存服务扣减库存


        } catch (Exception e) {
            responseResult.error500("下单异常");
            throw new RuntimeException("下单异常");
        }
        return responseResult.success("下单成功");
    }
}
